define(['api'],function (api) {
    var StaffTable = {
        name: 'staff-table',
        template: ' <div class="c-two-center c-layout">' +
        '        <i-table :columns="columnsHead" :data="data" size="small" ref="table" @on-select="onSelect"></i-table>' +
        '        <Modal' +
        '                title="删除员工"' +
        '                v-model="deletePos"' +
        '                :mask-closable="false"' +
        '                width="360"' +
        '                class-name="vertical-center-modal deleteWrap"' +
        '                ok-text="确认"' +
        '                @on-ok="handleSuccessDel">' +
        '            <div class="delete-position">' +
        '                <ul class="delete-ul">' +
        '                    <h3 class="warning">确认将该员工删除吗？</h3>' +
        '                </ul>' +
        '            </div>' +
        '        </Modal>' +
        '    </div>',
        created: function () {
            var that = this;
            // this.fetchListData();
            // bus.$on('on-search', function (tableData) {
            //   that.dataList = tableData
            // });
            bus.$on('export-data', function () {
                that.$refs.table.exportCsv({
                    filename: 'data',
                    columns: that.columnsHead.filter(function (col, index) {
                        console.log(col.key !== 'handle' && index > 1)
                        return col.key !== 'handle' && index > 1;
                    }),
                    data: that.data
                });
            });
        },
        props: {
            data: {
                type: Array,
                default: function () {
                    return [];
                }
            }
        },
        data: function () {
            var that = this;
            return {
                // dataList: [],
                tempDelDataId: '',
                deletePos: false,
                columnsHead: [
                    {
                        type: 'selection',
                        width: 60,
                        align: 'center'
                    },
                    {
                        title: '编号',
                        key: 'StaffNo'
                    },
                    {
                        title: '姓名',
                        key: 'Name',
                    },
                    {
                        title: '联系方式',
                        key: 'Phone'
                    },
                    {
                        title: '部门',
                        key: 'BelongToName'
                    },
                    {
                        title: '职位',
                        key: 'PositionName'
                    },
                    {
                        title: '角色权限',
                        key: 'PermissionName'
                    },
                    {
                        title: '管理归属',
                        key: 'Leader'
                    },
                    {
                        title: '员工类型',
                        key: 'StaffTypeName'
                    },
                    {
                        title: '操作',
                        key: 'handle',
                        render: function (h, params) {
                            return h('div', [
                                h('tooltip', {
                                    attrs: {
                                        placement: 'top',
                                        content: '编辑'
                                    }
                                }, [
                                    h('p', {
                                        attrs: {
                                            class: 'editStaff',
                                            type: 'primary'
                                        },
                                        on: {
                                            click: function () {
                                                bus.$emit('show-edit-modal', 'edit')
                                                bus.$emit('edit-staff', params.row)
                                            }
                                        }
                                    })
                                ]),
                                h('tooltip', {
                                    attrs: {
                                        placement: 'top',
                                        content: '删除'
                                    }
                                }, [
                                    h('p', {
                                        attrs: {
                                            class: 'cancelStaff',
                                            type: 'primary'
                                        },
                                        on: {
                                            click: function () {
                                                that.tempDelDataId = params.row.StaffId
                                                that.deletePos = true
                                            }
                                        }
                                    })
                                ])
                            ])
                        }
                    }
                ]
            }
        },
        methods: {
            search: function () {
                $.get(function (data) {
                    bus.$emit(data);//通过 bus 发布事件.
                });
            },
            showMemberModal: function (data) {
                // this.current
                console.log(this);
                // this.addNewStaffModal = true;
            },
            handleSuccessDel: function () {
                var that = this
                api.delStaff({StaffId: that.tempDelDataId})
                    .done(function (res) {
                        if (res.IsSuccess) {
                            that.data.forEach(function (item, index) {
                                if (item.StaffId === that.tempDelDataId) {
                                    that.data.splice(index, 1);
                                    that.$Message.success('删除成功');
                                }
                            });
                        } else {
                            that.$Message.error('删除失败');
                        }
                    })
                    .fail(function () {
                        that.$Message.error('删除失败');
                    })
            },
            onSelect: function (selection) {
                // this.$emit(selection, row);
                bus.$emit('on-selected', selection)
            }
        }
    };
    return StaffTable
})
